<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="交易对信息"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :label-width="formLabelWidth"
      :rules="rules"
    >
      <el-form-item label="交易对名称" prop="name">
        <el-input v-model="dialogForm.name" />
      </el-form-item>
      <el-form-item label="基础币名称" prop="base_coin_name">
        <el-input v-model="dialogForm.base_coin_name" />
      </el-form-item>
      <el-form-item label="基础币ID" prop="base_coin_id">
        <el-input v-model="dialogForm.base_coin_id" />
      </el-form-item>
      <el-form-item label="计价币名称" prop="quote_coin_name">
        <el-input v-model="dialogForm.quote_coin_name" />
      </el-form-item>

      <el-form-item label="计价币ID" prop="quote_coin_id">
        <el-input v-model="dialogForm.quote_coin_id" />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="dialogForm.is_active" />
      </el-form-item>
      <el-form-item label="id" style="display: none">
        <el-input v-model="dialogForm.symbol_id" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from "vue";
const dialogFormRef = ref(null);

defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  dialogForm: {
    type: Object,
    default: {
      symbol_id: "",
      name: "",
      base_coin_name: "",
      base_coin_id: "",
      quote_coin_name: "",
      quote_coin_id: "",
      is_active: false,
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits(["closeDialog", "cancel", "submitForm"]);

const formLabelWidth = "120px";
// 校验
const rules = {
  name: [{ required: true, message: "请输入交易对名称", trigger: "blur" }],
  base_coin_name: [
    { required: true, message: "请输入基础币名称", trigger: "blur" },
  ],
  base_coin_id: [
    { required: true, message: "请输入基础币ID", trigger: "blur" },
  ],
  quote_coin_name: [
    { required: true, message: "请输入计价币名称", trigger: "blur" },
  ],
  quote_coin_id: [
    { required: true, message: "请输入计价币ID", trigger: "blur" },
  ],
};

// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        const param = Object.assign({}, dialogForm);
        param.is_active = param.is_active ? 1 : 0;

        emits("submitForm", param);
      } catch (error) {
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};
</script>
